package com.droidclaimsurveyor.activity;

import java.util.List;

import com.droidclaimsurveyor.R;
import com.droidclaimsurveyor.model.CarImageModel;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class ImageListAdapter extends ArrayAdapter<CarImageModel> {
	private Context context;
	private int layoutId;
	private List<CarImageModel> dataList;
	
	public ImageListAdapter(Context context, int resource, List<CarImageModel> objects) {
		super(context, resource, objects);
		this.context = context;
		layoutId = resource;
		dataList = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		try{	
			View rowView = null;
			if(convertView==null){
				// create adapter layout
				view = new LinearLayout(getContext());
				String inflater = Context.LAYOUT_INFLATER_SERVICE;
				LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
				rowView = vi.inflate(layoutId, (LinearLayout) view, true);
			}else{
				rowView = convertView;
			}
			
			// set data to the TextView of each row
			ImageView imageView = (ImageView) rowView.findViewById(R.id.accident_imageview);
			Bitmap image = dataList.get(position).getImage();
			if(image!=null){
				imageView.setImageBitmap(image);
			}else{
				Drawable d = context.getResources().getDrawable(R.drawable.gallery);
				imageView.setImageDrawable(d);
			}
		}catch(Exception ex){
			Toast.makeText(context, ex.getMessage(), Toast.LENGTH_SHORT).show();
		}
		
		return view;
	}
}
